
const sql = require('mssql');
const baseFn = require('../../public/base.js');

let nowTimeHalf = baseFn.getNowTimeId().halfDate;

// 更新叫号状态
const updateCallNumberState = async (ctx) => {
    try {
        const { id, state } = ctx.request.body;
        let updateSql = ` update call_number set state = '${state}' WHERE id = '${id}' `;

        let result = await sql.query(updateSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '更新成功'
            }
        } else {
            ctx.body = {
                code: 400,
                message: '更新失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.originalError.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    }
}

// 获取正在排队的数量
const getWaitCount = async (ctx) => {
    try {
        let selectSql = `SELECT count(num.id) as total FROM call_number num left join BM_WLDW dw on num.supplier_code = dw.BM 
        WHERE get_number_time >= '${nowTimeHalf} 00:00:00' AND num.state = 'get' `;
        let result = await sql.query(selectSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '获取成功',
                total: result.recordset[0].total
            }
        } else {
            ctx.body = {
                code: 400,
                message: '获取失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.originalError.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    }
}



module.exports = {
    'GET /getWaitCount': getWaitCount,
    'POST /updateCallNumberState': updateCallNumberState
}